package com.zdya.combatdocuments.controller;

import com.zdya.combatdocuments.entity.User;
import com.zdya.combatdocuments.entity.vo.ResultVo;
import com.zdya.combatdocuments.entity.vo.UserLoginVo;
import com.zdya.combatdocuments.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;


@RestController
@RequestMapping("/documents/user")
@Api(tags = "用户",description = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "User",value = "账户，密码，JSON格式")
    })
    @PostMapping("/login")
    public ResultVo<UserLoginVo> login(@RequestBody User userEntity){
        return userService.login(userEntity);
    }

    @GetMapping("/logout")
    public ResultVo logout(){
        return userService.logout();
    }

    @ApiOperation(value = "获取本机CPU和MAC",httpMethod = "GET")
    @GetMapping("/getMacAndCpu")
    public ResultVo getMacAndCpu(){
        return userService.findMacAndCpu();
    }

    @ApiOperation(value = "通讯录",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "登录用户id，Restful风格"),
            @ApiImplicitParam(name = "name",value = "查询某个人，可不填"),
    })
    @GetMapping("/userAddress/{id}")
    public ResultVo<LinkedHashMap<String, List<User>>> UserAddress(@PathVariable("id") Long id, String name){
        return userService.getUserAndName(id,name);
    }

    @ApiOperation(value = "修改密码",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "User",value = "用户信息，JSON格式")
    })
    @PutMapping("/update")
    public ResultVo<Object> updateUser(@RequestBody User userEntity){
        return userService.updateUser(userEntity);
    }

    @ApiOperation(value = "查看详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户账号")
    })
    @GetMapping("/details")
    public ResultVo<User> details(String username){
        return userService.getDetails(username);
    }

    @PostMapping("/upload/{id}")
    public ResultVo upload(@PathVariable("id") Long id,MultipartFile file, HttpServletRequest request){
      return  userService.upload(id,file,request);
    }

    @PostMapping("/save")
    public ResultVo save(@RequestBody User user){
        return userService.saveUser(user);
    }

    @ApiOperation(value = "上传授权文件",httpMethod = "POST")
    @PostMapping("/uploadFile")
    public ResultVo uploadFile(MultipartFile file){
        return userService.uploadFile(file);
    }
}
